package com.mobiletrain.org.motherkitch.fenlei;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mobiletrain.org.motherkitch.MyApplication;
import com.mobiletrain.org.motherkitch.R;
import com.mobiletrain.org.motherkitch.fenlei.adapter.ThirdList2Adapter;
import com.mobiletrain.org.motherkitch.fenlei.adapter.ThirdList3Adapter;
import com.mobiletrain.org.motherkitch.fenlei.adapter.ThirdListAdapter;
import com.mobiletrain.org.motherkitch.fenlei.bean.ShouCang;
import com.mobiletrain.org.motherkitch.goumaiqingdan.ShoppingList1;
import com.mobiletrain.org.motherkitch.utils.ImageLoad;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import com.mobiletrain.org.motherkitch.fenlei.bean.Cook;
import com.mobiletrain.org.motherkitch.fenlei.bean.Reply;
import com.mobiletrain.org.motherkitch.fenlei.bean.Stage;
import com.mobiletrain.org.motherkitch.fenlei.bean.ThirdItem;
import com.mobiletrain.org.motherkitch.fenlei.bean.User;


public class ThridActivity extends ActionBarActivity {

    private ThirdItem third;
    private Reply reply;
    private Cook cook;
    private User user;
    private Stage stage;
    private ArrayList<ThirdItem> thirds = new ArrayList<ThirdItem>();
    private ArrayList<Reply> replys = new ArrayList<Reply>();
    private ArrayList<Cook> cooks = new ArrayList<Cook>();
    private ArrayList<User> users = new ArrayList<User>();
    private ArrayList<Stage> stages = new ArrayList<Stage>();
    private BitmapUtils bitmapUtils;
    private ImageView icon;
    private TextView title,text1,text2,text3,content;
    private ListView list1,list,list2,list3;
    private ArrayList<HashMap<String,String>>arr=new ArrayList<HashMap<String,String>>();
    private String TAG="----------------------";
    private Button btn1,btn2,btn3;
    private Toolbar bar;
    private DbUtils db;
    private Button mBtnBuy;
    private ShoppingList1 sl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thrid);
        bitmapUtils = ImageLoad.getBitmapUtils(this);
        bar = (Toolbar) findViewById(R.id.third_toolbar);
        setSupportActionBar(bar);
        ImageView ig= (ImageView) findViewById(R.id.toolbarImage);
        ig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThridActivity.this.finish();
            }
        });
        Bundle bundle = getIntent().getBundleExtra("key");
        db=DbUtils.create(this);
        final ShouCang sou=new ShouCang();
        String id = bundle.getString("id");
        sou.setId(id);
        sou.setIcon(bundle.getString("pic"));
        sou.setTitle(bundle.getString("title"));
        /*sou.setCai(bundle.getString("cai"));
        sou.setNum(bundle.getString("num"));*/

        init();
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    db.save(sou);
                    Toast.makeText(ThridActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        });
        jsonjiexi(id);
        mBtnBuy= (Button) findViewById(R.id.third_btn2);
        mBtnBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication application=new MyApplication();
                DbUtils db = application.getDbUtils();

                try {
                    db.save(sl);
                    Toast.makeText(ThridActivity.this,"添加清单成功!",Toast.LENGTH_SHORT).show();
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }

        });



    }

    private void jsonjiexi(String id1) {

        String path = "http://www.qubaobei.com/ios/cf/dish_view.php?v=1&id=" + id1+ "&user_id=4828718";
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET, path, new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                String json = objectResponseInfo.result.toString();
                try {
                    sl=new ShoppingList1();
                    JSONObject obj = new JSONObject(json);
                    JSONObject obje = obj.getJSONObject("data");
                    JSONObject j = obje.getJSONObject("num_info");
                    third = new ThirdItem();
                    third.setTitle(obje.getString("title"));
                    title.setText(obje.getString("title"));
                    third.setContent(obje.getString("content"));
                    content.setText(obje.getString("content"));
                    third.setPic(obje.getString("pic"));
                    bitmapUtils.display(icon,obje.getString("pic"));
                    sl.setTitle(obje.getString("title"));
                    sl.setId(obje.getString("id"));

                    third.setCook_num(j.getString("cook_num"));
                    text1.setText(j.getString("cook_num") + "作品");
                    third.setReply_num(j.getString("reply_num"));
                    text3.setText(j.getString("reply_num") + "评论");
                    third.setCollect_num(j.getString("collect_num"));
                    text2.setText(j.getString("collect_num") + "收藏");


                    JSONArray stage1 = obje.getJSONArray("stage");
                    for (int i = 0; i < stage1.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        stage = new Stage();
                        JSONObject oj = stage1.getJSONObject(i);
                        stage.setStage_id(oj.getString("stage_id"));
                        map.put("name", oj.getString("stage_id"));
                        stage.setContent(oj.getString("content"));
                        map.put("content", oj.getString("content"));
                        arr.add(map);
                        stages.add(stage);
                    }
                    SimpleAdapter adapter = new SimpleAdapter(ThridActivity.this,arr, R.layout.fenlei_third_list_item,
                            new String[]{"name","content"}, new int[]{R.id.third_item_text, R.id.third_item_text1});
                    list1.setAdapter(adapter);
                    JSONArray use1 = obje.getJSONArray("use");
                    for (int i = 0; i < use1.length(); i++) {
                        user = new User();
                        JSONObject oj1 = use1.getJSONObject(i);
                        user.setName(oj1.getString("name"));
//                        Log.i(TAG,oj1.getString("name"));
                        user.setNum(oj1.getString("num"));
                        users.add(user);
                        sl.setName(oj1.getString("name"));
                        sl.setNum(oj1.getString("num"));
                    }
//                    Toast.makeText(ThridActivity.this,"111111111",Toast.LENGTH_SHORT).show();

                    ThirdListAdapter ada = new ThirdListAdapter(ThridActivity.this,users);
                    ada.notifyDataSetChanged();
                    list.setAdapter(ada);
                    third.setUser(users);
                    JSONArray cook1 = obje.getJSONArray("cook");
                    for (int i = 0; i < cook1.length(); i++) {
                        cook = new Cook();
                        JSONObject oj = cook1.getJSONObject(i);
                        cook.setPic(oj.getString("pic"));
                        cook.setContent(oj.getString("content"));
                        JSONObject ob = oj.getJSONObject("user");
                        cook.setNickname(ob.getString("nickname"));
                        cook.setAvatar(ob.getString("avatar"));
                        cook.setAge_str(ob.getString("age_str"));
                        cook.setShow_date(oj.getString("show_date"));
                        cooks.add(cook);
                    }
                    ThirdList2Adapter adapter1 = new ThirdList2Adapter(cooks, ThridActivity.this);
                    list2.setAdapter(adapter1);
                    third.setCook(cooks);
                    JSONArray reply1 = obje.getJSONArray("reply");
                    for (int i = 0; i < reply1.length(); i++) {
                        reply = new Reply();
                        JSONObject oj = reply1.getJSONObject(i);
                        reply.setContent(oj.getString("content"));
                        JSONObject ob = oj.getJSONObject("user");
                        reply.setNickname(ob.getString("nickname"));
                        reply.setAvatar(ob.getString("avatar"));
                        reply.setAge_str(ob.getString("age_str"));
                        reply.setShow_date(oj.getString("show_date"));
                        replys.add(reply);
                    }

                    third.setReply(replys);
                    ThirdList3Adapter adap = new ThirdList3Adapter(ThridActivity.this, replys);
                    list3.setAdapter(adap);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    private void init() {
        icon = (ImageView) findViewById(R.id.thrid_icon);
        title= (TextView) findViewById(R.id.third_title);
        text1= (TextView) findViewById(R.id.third_text1);
        text2= (TextView) findViewById(R.id.third_text2);
        text3= (TextView) findViewById(R.id.third_text3);
        btn1= (Button) findViewById(R.id.third_btn1);
        btn2= (Button) findViewById(R.id.third_btn2);
        btn3= (Button) findViewById(R.id.third_btn3);
        content= (TextView) findViewById(R.id.third_content);
        list1= (ListView) findViewById(R.id.third_list1);
        list= (ListView) findViewById(R.id.third_list);
        list2= (ListView) findViewById(R.id.third_list2);
        list3= (ListView) findViewById(R.id.third_list3);
    }



}
